package com.housekeeping.housekeeping.administrator.adminsinformation.dao.impl;

import com.housekeeping.housekeeping.administrator.adminsinformation.dao.SearchDao;
import com.housekeeping.housekeeping.administrator.entity.AdminsInformation;
import com.housekeeping.housekeeping.administrator.utils.Dbmanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SearchDaoImpl implements SearchDao {
    @Override
    public List<AdminsInformation> search(String keyword) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<AdminsInformation> list =new ArrayList<>();
        try {
            connection= Dbmanage.getConnection();
            String sql = "select *from admins_information where name like "+"'"+"%" + keyword + "%"+"'";
            statement=connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            while (resultSet.next()){
                AdminsInformation adminsInformation = new AdminsInformation();
                adminsInformation.setId(resultSet.getInt(1));
                adminsInformation.setName(resultSet.getString(2));
                adminsInformation.setAccount(resultSet.getString(3));
                adminsInformation.setPassword(resultSet.getString(4));
                adminsInformation.setPhoneNumber(resultSet.getString(5));
                adminsInformation.setJurisdiction(resultSet.getString(6));
                adminsInformation.setRemark(resultSet.getString(7));
                adminsInformation.setDate(resultSet.getString(8));
                list.add(adminsInformation);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            Dbmanage.close(connection,statement,resultSet);
        }
        return list;
    }
}
